from flask import Flask
from config import Config
import os
import logging
from pathlib import Path

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)
    
    # 确保上传文件夹存在
    os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
    
    from app.routes import main
    app.register_blueprint(main)
    
    return app 

def setup_logging():
    """设置日志"""
    # 创建日志目录
    log_dir = Path.home() / '.lora_filter' / 'logs'
    log_dir.mkdir(parents=True, exist_ok=True)
    
    # 设置日志文件
    log_file = log_dir / 'app.log'
    
    # 配置日志格式
    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s [%(levelname)s] %(message)s',
        handlers=[
            logging.FileHandler(log_file, encoding='utf-8'),
            logging.StreamHandler()
        ]
    )
    
    # 设置第三方库的日志级别
    logging.getLogger('PIL').setLevel(logging.WARNING)
    logging.getLogger('cv2').setLevel(logging.WARNING)

# 初始化日志
setup_logging()
logger = logging.getLogger(__name__) 